﻿using Storage.Admin.Framework.Controllers.Admin;
using Storage.Admin.Framework.Menu;
using Storage.Core.Extensions;
using Storage.Mapping;
using Storage.Mapping.Bucket;
using Storage.Mapping.BucketCut;
using Storage.Services.AppStorage;
using Storage.Services.Bucket;
using Storage.Services.BucketCut;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Storage.Admin.Areas.Admin.Controllers
{
    [RoutePrefix("bucket")]
    public class BucketController : BaseAdminController
    {
        // GET: Admin/Bucket
        private IBucketService _bucketService;
        private IAppStorageService _appStorageService;
        private IBucketCutService _bucketCutService;

        public BucketController(IBucketService bucketService,
            IAppStorageService appStorageService,
            IBucketCutService bucketCutService)
        {
            this._bucketCutService = bucketCutService;
            this._bucketService = bucketService;
            this._appStorageService = appStorageService;
        }

        /// <summary>
        /// bucket列表
        /// </summary>
        /// <returns></returns>
        [Route("", Name = "bucketIndex")]
        [Descriper("Bucket列表预览", true, "menu-icon fa fa-caret-right", FatherResource = "Storage.Admin.Areas.Admin.Controllers.SystemManageController")]
        public ActionResult BucketIndex()
        {
            var list = _bucketService.getAllCache();
            var appList = _appStorageService.getAllCache();
            if (list != null && appList != null)
            {
                var cutList = _bucketCutService.getAllCach();

                list = list.Join(appList, a => a.AppStorageId, b => b.Id, (a, b) => new BucketMapping()
                {
                    Id = a.Id,
                    Name = a.Name,
                    AppStorage = b,
                    CreationTime = a.CreationTime,
                    AppStorageId = a.AppStorageId,
                    BucketCutList = cutList != null ? cutList.Where(o => o.BucketId == a.Id).ToList() : null
                }).ToList();
            }
            return View(list);
        }

        /// <summary>
        /// 添加Bucket
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("edit", Name = "editBucket")]
        [Descriper("添加Bucket", false, FatherResource = "Storage.Admin.Areas.Admin.Controllers.BucketController.BucketIndex")]
        public ActionResult EditBucket()
        {
            BucketMapping model = new BucketMapping();
            var appList = _appStorageService.getAllCache();
            if (appList == null || !appList.Any())
                return RedirectToRoute("editAppStorage");
            ViewBag.AppList = _appStorageService.getAllCache();
            return View(model);
        }
        [HttpPost]
        [Route("edit")]
        public JsonResult EditBucket(BucketMapping model)
        {
            if (!ModelState.IsValid)
            {
                AjaxData.Message = ModelState.getErrMsg();
                return Json(AjaxData, JsonRequestBehavior.DenyGet);
            }
            var entity = model.toEntity();
            entity.Id = Guid.NewGuid();
            entity.CreationTime = DateTime.Now;
            _bucketService.insertBucket(entity);
            AjaxData.Status = true;
            AjaxData.Message = "添加成功";
            return Json(AjaxData, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 远程验证名称是否已经存在
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [Route("exist-name", Name = "existBucketName")]
        [Descriper("验证名称是否已经存在", false, FatherResource = "Storage.Admin.Areas.Admin.Controllers.SystemManageController")]
        public JsonResult ExistBucketName(Guid id, string name)
        {
            return Json(!_bucketService.existName(id, name), JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 添加BucketCut
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("editCut", Name = "editBucketCut")]
        [Descriper("添加BucketCut剪裁尺寸", false, FatherResource = "Storage.Admin.Areas.Admin.Controllers.BucketController.BucketIndex")]
        public ActionResult EditBucketCut(Guid bucketId)
        {
            BucketCutMapping model = new BucketCutMapping() { BucketId = bucketId };
            return PartialView(model);
        }
        [HttpPost]
        [Route("editCut")]
        public JsonResult EditBucketCut(BucketCutMapping model)
        {
            if (!ModelState.IsValid)
            {
                AjaxData.Message = ModelState.getErrMsg();
                return Json(AjaxData, JsonRequestBehavior.DenyGet);
            }
            var entity = model.toEntity();
            entity.Id = Guid.NewGuid();
            entity.CreationTime = DateTime.Now;
            entity.Creator = WorkContext.CurrentUser.Id;
            _bucketCutService.insertBucketCut(entity);
            AjaxData.Status = true;
            AjaxData.Message = "添加成功";
            return Json(AjaxData, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("delCut",Name = "deleteCut")]
        [Descriper("删除尺寸", false, FatherResource = "Storage.Admin.Areas.Admin.Controllers.BucketController.BucketIndex")]
        public JsonResult DeleteCut(Guid id)
        {
            _bucketCutService.deleteBucketCut(id);
            AjaxData.Status = true;
            AjaxData.Message = "删除成功";
            return Json(AjaxData, JsonRequestBehavior.AllowGet);
        }

    }
}